<?php
namespace app\index\taglib;
use think\template\TagLib;
class Mytag extends TagLib{

    /**
     * 定义标签列表
     */
    protected $tags   =  [
        // 标签定义： attr 属性列表 close 是否闭合（0 或者1 默认1） alias 标签别名 level 嵌套层次
        'goods'     => ['attr' => 'type,num,name,titleLen,cid', 'close' => 1], //闭合标签，默认为不闭合
        'open'      => ['attr' => 'name,type', 'close' => 1], 
        
    ];

    /**
     * 这是一个闭合标签的简单演示
     */
    public function tagGoods($tag,$content){
        
        $type = isset($tag['type']) ? $tag['type'] : 'new';
        $num = isset($tag['num']) ? $tag['num']+0 : 5;
        $name = isset($tag['name']) ? $tag['name'] : 'good';
        $titleLen = isset($tag['titleLen']) ? $tag['titleLen'] : '30';
        $cid = isset($tag['cid']) ? $tag['cid']+0 : 0;

        $where = 'is_on_sale = 1 AND is_delete = 0';

        if( $type == 'promote' ){

            $where .= ' AND is_promote = 1';

        }else if( $type == 'hot' ){

            $where .= ' AND is_hot = 1';

        }else if( $type == 'best' ){

            $where .= ' AND is_best = 1';

        }else if( $type == 'new' ){

            $where .= ' AND is_new = 1';

        }
    
        $parse = '<?php ';
        if( $type == 'like' ){

            $parse .= '$model = new \app\index\model\Goods();';
            $parse .= '$__LIST__ = $model->getRecentviewedGoods('.$cid.','.$num.');';

        }else{

            $field = 'goods_name,static_url,shop_price,is_promote,promote_start_time,promote_end_time,promote_price,pic_small';

            // 是否传递了cid
            if( $cid ){

                $where .= ' AND ( g.cid = '.$cid.' OR gc.cid ='.$cid.' )';

                $field .= ',g.gid';

                $parse .= '$__LIST__ = db("goods as g")->join("jd_goods_category gc","g.gid = gc.gid","LEFT")->field("'.$field.'")->where("'.$where.'")->limit('.$num.')->select();';

            }else{

                $field .= ',gid';
                $parse .= '$__LIST__ = db("goods")->field("'.$field.'")->where("'.$where.'")->limit('.$num.')->select();';
            }

        }
        $parse .= ' ?>';
        $parse .= '{volist name="__LIST__" id="' . $name . '"}';
        $parse .= '<?php 
                    $title = mb_substr($'.$name.'["goods_name"], 0, '.$titleLen.', "utf-8");
                    if( strlen($title) < strlen( $'.$name.'["goods_name"] ) ){ 
                        $'.$name.'["goods_name"] = $title."..."; 
                    };
                    if($'.$name.'["is_promote"]== 1 && $'.$name.'["promote_start_time"] < time() && $'.$name.'["promote_end_time"] > time() ){ 
                        $'.$name.'["price"] = $'.$name.'["promote_price"];
                    }else if(session("mid")){ 
                        $lv = session("mid") ? session("mid") : 0;
                        $price = db("member_price")->where( ["gid" => $'.$name.'["gid"],"lid" => $lv] )->value("member_price");
                        if( $price > 0  ){ 
                            
                            $'.$name.'["price"] = $price; 
                        }else if( $price == 0  ){ 
                            $level = config("member.level");
                            $rate = $level[$lv][2]/100;
                            $'.$name.'["price"] = round( $'.$name.'["shop_price"] * $rate ).".00"; 
                        }else{ 
                            $'.$name.'["price"] = $'.$name.'["shop_price"];
                        } 

                    }else{ 
                            $'.$name.'["price"] = $'.$name.'["shop_price"];
                    };';
        $parse .= ' ?>';
        $parse .= $content;
        $parse .= '{/volist}';

        return $parse;
    }
    
    /**
     * 这是一个非闭合标签的简单演示
     */
    public function tagOpen($tag, $content)
    {
        $type = empty($tag['type']) ? 0 : 1; // 这个type目的是为了区分类型，一般来源是数据库
        $name = $tag['name']; // name是必填项，这里不做判断了
        $parse = '<?php ';
        $parse .= '$test_arr=[[1,3,5,7,9],[2,4,6,8,10]];'; // 这里是模拟数据
        $parse .= '$__LIST__ = $test_arr[' . $type . '];';
        $parse .= ' ?>';
        $parse .= '{volist name="__LIST__" id="' . $name . '"}';
        $parse .= $content;
        $parse .= '{/volist}';
        return $parse;
    }
   
}
